package com.yupi.usercenter.controller;


import com.yupi.usercenter.model.User;
import com.yupi.usercenter.model.request.UserLoginRequest;
import com.yupi.usercenter.model.request.UserRegisterRequest;
import com.yupi.usercenter.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    private UserService userService;
//    @Resource
//    private RabbitTemplate rabbitTemplate;
  /*  @GetMapping("/send")
    public String sendRabbitmq() {
        float delayHour = 2f;
        CustomerAutoUnassignMessage customerAutoUnassignMessage = new CustomerAutoUnassignMessage("wangbo");
        rabbitTemplate.convertAndSend("wangbotestexchange", null, customerAutoUnassignMessage, processor -> {
            processor.getMessageProperties().setContentEncoding("UTF-8");
            processor.getMessageProperties().setHeader("x-delay", delayHour * 60 * 1000);
            return processor;
        });
        return "success";
    }*/

    // 注册
    @PostMapping("/register")
    public long userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null){
            return -1;
        }
        return userService.userRegister(userRegisterRequest.getUserAccount(), userRegisterRequest.getUserPassword(), userRegisterRequest.getCheckPassword());
    }
    // 登录
    @PostMapping("/login")
    public User userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null){
            return null;
        }
        return userService.userLogin(userLoginRequest.getUserAccount(), userLoginRequest.getUserPassword(), request);
    }
    //查询用户，根据用户名查询
    @GetMapping("/search")
    public List<User> searchUserByName(@RequestParam("username") String username, HttpServletRequest request) {
        Object user = request.getSession().getAttribute("user");
        User user1 = (User) user;
        if (user1 == null ||user1.getUserRole()!=1){
            //不是管理员
            return new ArrayList<>();
        }
        return userService.searchUserByName(username);
    }
    //删除用户，根据id删除
    @DeleteMapping("/delete")
    public boolean deleteUser(@RequestParam("id") long id) {
        if (id<=0){
            return false;
        }
        return userService.removeById(id);
    }

}
